#!/usr/bin/env python
import pygame
import sys
from random import *
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from random import *
import gl
import particles
import game

font = None
psystem = None
c = None
help = """h - show this message for some time
k - kill all particles
s - add smoke emitter
e - add explosion"""
help_timeout = 0
fps_timeout = 0

def init(w, h):
    pygame.display.set_mode((w, h), DOUBLEBUF | OPENGL)
    pygame.display.set_caption('PyOpengl particles examples')
    
    print pygame.display.Info()
    
    gl.glInit(w, h)

def draw():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_TEXTURE_2D)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE)
    x0, y0 = 0, 0
    s0 = 0
    r0 = 0
    for s in psystem:
        glLoadIdentity()
        glTranslate(s.rect.x, s.rect.y, 0)
        glRotate(s.heading, 0.0, 0.0, 1.0)
        glScalef(s.growth, s.growth, 1.0)
        s._image._draw_quad()
    pygame.display.flip()
    
    #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    #glEnable(GL_TEXTURE_2D)
    #psystem.draw(pygame.Rect(0, 0, 600, 400))
    #font.render(str(c.get_fps()) + "\n" + str(len(psystem)), (0, 400 - font.line_heigth))
    #if help_timeout > pygame.time.get_ticks():
    #    font.render(help, (0, 400 - font.line_heigth * 3))
    #pygame.display.flip()
    
def loadImage(name):
    img = pygame.image.load(name)
    
    return gl.GLSurface(img)
    
def main_loop():
    global help_timeout, fps_timeout
    time = pygame.time.get_ticks() + 1000*60
    for i in xrange(0, 250):
        psystem.add(particles.FireParticle((randint(200, 400), randint(100, 300)),
            (0,0), random() - 0.5, 0.01, 1, 60000))
    while(pygame.time.get_ticks() < time):
        c.tick(30)
        for event in pygame.event.get():
            if event.type == QUIT or event.type == KEYUP and event.key == K_ESCAPE:
                return
        
        k = pygame.key.get_pressed()
        if k[K_h]:
            help_timeout = pygame.time.get_ticks() + 3000
        if k[K_s]:
            p = list(pygame.mouse.get_pos())
            p[1] = 400 - p[1]
            psystem.add_emitter(
                particles.SmokeEmitter(1, p, (70, 30),
                                       (3, 3 + random() * 10),
                                       100))
        if k[K_e]:
            p = list(pygame.mouse.get_pos())
            p[1] = 400 - p[1]
            psystem.add_emitter(
                particles.Explosion(p, (1, 1 + random()),
                                    (0, 0), 300))
        if k[K_k]:
            psystem.empty()
            
                    
        psystem.update(1)
        draw()
        
        if fps_timeout - pygame.time.get_ticks() < 0:
            print(str(c.get_fps()) + "_fps\t" + str(len(psystem)) + "_particles")
            fps_timeout = 3000 + pygame.time.get_ticks()

if __name__ == '__main__':
    try:
        import psyco
        #psyco.log()
        psyco.full()
    except ImportError, message:
        print message
    init(600, 400)
    c = pygame.time.Clock()
    psystem = particles.ParticleSystem(500)
    font = gl.GLFont()
    help_timeout = pygame.time.get_ticks() + 3000
    game.vp = 0, 0
    game.zoom = 1
    main_loop()
    #import hotshot, hotshot.stats, test.pystone
    #prof = hotshot.Profile("pyship.prof") # file to store the results
    #benchtime = prof.run('main_loop()') # run the method
    #prof.close() # done
    #stats = hotshot.stats.load("pyship.prof") # load the results
    #stats.strip_dirs() 
    #stats.sort_stats('time', 'calls') # sort by time invested on each method
    #stats.print_stats(20)
